﻿using Yitter.IdGenerator;

namespace Admin.NET.Application.Service;
/// <summary>
/// 输血申请单
/// </summary>
public partial class TraShenQingService : BaseServiceBill<TraShenQing>
{
    private readonly SqlSugarRepository<TraShenQingHuanZhe> _repTraShenQingHuanZhe;
    private readonly SqlSugarRepository<TraShenQingYongXue> _repTraShenQingYongXue;
    private readonly SqlSugarRepository<TraShenQingJianYan> _traShenQingJianYanRep;
    private readonly SysAuthService _sysAuthService;
    private readonly BasEnumService _basEnumService;
    private readonly SqlSugarRepository<TraPeiXue> _traPeiXue;
    private readonly SqlSugarRepository<TraPeiXueMingXi> _traPeiXueMingXi;
    private readonly SqlSugarRepository<TraFuHe> _traFuHe;
    private readonly SqlSugarRepository<TraFaXue> _traFaXue;
    private readonly SqlSugarRepository<TraFaXueMingXi> _traFaXueMingXi;
    private readonly SysUserService _sysUserService;
    private readonly UserManager _userManager;
    public TraShenQingService(IServiceScopeFactory scopeFactory, SqlSugarRepository<TraShenQingHuanZhe> repTraShenQingHuanZhe,
        SqlSugarRepository<TraShenQingYongXue> repTraShenQingYongXue, SqlSugarRepository<TraShenQingJianYan> traShenQingJianYanRep, BasEnumService basEnumService, SysAuthService sysAuthService,
        SqlSugarRepository<TraPeiXue> traPeiXue, SqlSugarRepository<TraFuHe> traFuHe, SysUserService sysUserService, UserManager userManager,
        SqlSugarRepository<TraFaXue> traFaXue, SqlSugarRepository<TraFaXueMingXi> traFaXueMingXi, SqlSugarRepository<TraPeiXueMingXi> traPeiXueMingXi) : base(scopeFactory)
    {
        _repTraShenQingHuanZhe = repTraShenQingHuanZhe;
        _repTraShenQingYongXue = repTraShenQingYongXue;
        _traShenQingJianYanRep = traShenQingJianYanRep;
        _basEnumService = basEnumService;
        _sysAuthService = sysAuthService;
        _traPeiXue = traPeiXue;
        _traFuHe = traFuHe;
        _sysUserService = sysUserService;
        _userManager = userManager;
        _traFaXue = traFaXue;
        _traFaXueMingXi = traFaXueMingXi;
        _traPeiXueMingXi = traPeiXueMingXi;
    }


    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<TraShenQingPageOutput>> Page(BaseKeywordPageInput input)
    {
        var traShenQingOutputList = await Queryable.FuzzyQuery(input.Keyword, new List<string> { "Id" })
           .LeftJoin<TraShenQingHuanZhe>((it, h) => it.Id == h.Id)
           .LeftJoin<TraShenQingYongXue>((it, h, y) => it.Id == y.Id)
           .Select<TraShenQingPageOutput>()
           .OrderByDescending(it => it.Id)
           .ToPagedListAsync(input.Page, input.PageSize);

        return traShenQingOutputList;
    }

    /// <summary>
    /// 通过Id获取
    /// </summary>
    public TraShenQingDanOutput Get(string id)
    {
        var traShenQing = GetById<TraShenQingOutput>(id);
        var traShenQingHuanZhe = _repTraShenQingHuanZhe.InSingle(id);
        var traShenQingYongXue = _repTraShenQingYongXue.InSingle(id);
        var traShenQingJianYanList = _traShenQingJianYanRep.Queryable.Where(x => x.traShenQingId == id).ToList();
        return new TraShenQingDanOutput
        {
            traShenQing = traShenQing,
            traShenQingHuanZhe = traShenQingHuanZhe.Adapt<TraShenQingHuanZheOutput>(),
            traShenQingYongXue = traShenQingYongXue.Adapt<TraShenQingYongXueOutput>(),
            traShenQingJianYanList = traShenQingJianYanList
        };
    }

    /// <summary>
    /// 保存申请单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public TraShenQing Add(TraShenQingDanInput input)
    {
        input.traShenQing.Id = CreateId();
        input.traShenQing.danJuZhuangTai = DanJuZhuangTai.待上级审核;

        //主表
        var entity = AddWithInput(input.traShenQing);

        //患者信息
        input.traShenQingHuanZhe.Id = input.traShenQing.Id;
        _repTraShenQingHuanZhe.InsertReturnEntity(input.traShenQingHuanZhe.Adapt<TraShenQingHuanZhe>());

        //用血信息
        input.traShenQingYongXue.Id = input.traShenQing.Id;
        _repTraShenQingYongXue.InsertReturnEntity(input.traShenQingYongXue.Adapt<TraShenQingYongXue>());

        //检验信息
        SaveJianYan(input.traShenQingJianYanList, input.traShenQing.Id);

        return entity;
    }

    public bool Update(TraShenQingDanInput input)
    {
        //主表
        UpdateWithInput(input.traShenQing);

        //患者信息
        input.traShenQingHuanZhe.Id = input.traShenQing.Id;
        UpdateHuanZhe(input.traShenQingHuanZhe);

        //用血信息
        input.traShenQingYongXue.Id = input.traShenQing.Id;
        UpdateYongXue(input.traShenQingYongXue);

        //检验信息( 删除后重新插入 )
        SaveJianYan(input.traShenQingJianYanList, input.traShenQing.Id);

        return true;
    }

    /// <summary>
    /// 创建输血申请单Id
    /// </summary>
    private string CreateId()
    {
        // 暂时用雪花, 需要修改为 日期+3位流水
        return YitIdHelper.NextId() + "";
    }

    /// <summary>
    /// 保存申请单检验结果
    /// </summary>
    /// <param name="list">申请单列表</param>
    /// <param name="traShenQingId">申请单号</param>
    private void SaveJianYan(List<TraShenQingJianYanInput> list, string traShenQingId)
    {
        // 先当前申请单的检验结果
        _traShenQingJianYanRep.Delete(x => x.traShenQingId == traShenQingId);

        // 再全部插入
        if (null != list && list.Count() > 0)
        {
            foreach (var item in list)
            {
                item.traShenQingId = traShenQingId;
            }
            _traShenQingJianYanRep.InsertRange(list.Adapt<List<TraShenQingJianYan>>());

            // 操作记录
            AddOptBill(traShenQingId, "检验结果", list);
        }
    }
}
